<?php

namespace App\Admin\Forms;

use App\Jobs\CreateActiveCode;
use App\Models\ActiveCode;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;

class SendActiveCode extends Form
{
    use LazyWidget;
    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        // 获取当前行ID
        $id = $this->payload['id'] ?? null;

        $number = $input['number'];
        for ($i = 1; $i <= $number; $i++) {
            $a = new ActiveCode();
            $a->user_id = $id;
            $a->save();
        }
        ActiveCode::whereNull('code')->chunk(100,function ($codes){
            foreach($codes as $code){
                CreateActiveCode::dispatch($code)->onQueue('create-active-code');
            }
        });
        return $this->response()->success('发放成功')->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->number('number', '激活码数量')->rules('required');
    }
}
